<template>
  <div class="login">
    <loginparticles/>
    <div class="login-box">
      <p class="title">“信息交流”后台管理系统</p>
      <form action id="form" @submit.prevent="onSubmit">
        <p class="usericon">
          <el-input id="username" type="text" prefix-icon="el-icon-user" v-model="username" placeholder="用户"></el-input>
        </p>
        <p class="passicon">
          <el-input id="password" type="password" prefix-icon="el-icon-lock" v-model="password" placeholder="密码"></el-input>
        </p>
        <p>
          <el-button type="submit" @click.prevent="login" class="submit-btn">立即登录</el-button>
        </p>
        <div class="others">
          <!-- <a href class="register">立即注册</a> -->
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import Loginparticles from './loginparticles'
import { mapActions } from 'vuex'

export default {
  name: 'login',
  components: { Loginparticles },
  data () {
    return {
      password: 'admin',
      username: 'admin'
    }
  },
  methods: {
    ...mapActions(['setUserInfo', 'setToken']),
    // 登录
    login () {
      if (this.password === '' || this.password === '') {
        this.$message.error('账号或者密码为空')
        return
      }
      const obj = {
        password: this.password,
        username: this.username
      }
      this.$axios.post('/admin/login', this.qs.stringify(obj))
        .then(result => {
          const data = result.data.data
          if (result.data.state.type === 'SUCCESS') {
            this.$message.success('登录成功')
            this.setUserInfo(data.userInfo)
            this.setToken(data.token)
            this.$router.push('/')
          } else this.$message.error('用户名或密码错误')
        }).catch(e => {
          this.$message(e)
        })
    }
  }
}
</script>

<style scoped>
  .login-box {
    width: 300px;
    height: 250px;
    padding: 20px;
    box-shadow: 0 0 30px #e0b3e9;
    margin: auto;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
  }
  .login-box p.title {
    font-size: 23px;
    font-weight: bold;
    text-align: center;
    color: #90000a;
    text-shadow: 0 0 1px #80001b;
    margin-bottom: 30px;
  }
  .login-box p {
    position: relative;
  }
  .login-box p::before {
    font-family: "adminthemesregular";
    position: absolute;
    top: 0;
    left: 10px;
    height: 42px;
    line-height: 42px;
    font-size: 20px;
    color: #90000a;
  }
  canvas {
    position: absolute;
  }
  .login-box el-input {
    font-size: 16px;
    height: 26px;
    width: 90%;
    border: none;
    padding: 8px 5%;
    text-indent: 2em;
  }
  .login-box el-input::-webkit-input-placeholder {
    color: #f4f4f4;
    line-height: inherit;
  }
  .login-box el-input::-moz-placeholder {
    color: #f4f4f4;
    line-height: inherit;
  }
  .login-box .submit-btn {
    width: 100%;
    height: 42px;
    font-size: 16px;
    text-indent: 0;
  }
  .others {
    position: relative;
    margin-top: 2px;
  }
</style>
